package com.itheima.wisdomnext.mapper;

import com.itheima.wisdomnext.entity.Follow;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FollowMapper {

    // 获取用户关注的所有记录
    @Select("SELECT * FROM follows WHERE follower_id = #{userId}")
    List<Follow> getAllFollowers(Integer userId);

    // 获取用户的所有被关注记录
    @Select("SELECT * FROM follows WHERE followed_id = #{userId}")
    List<Follow> getAllFollowed(Integer userId);

    // 根据关注者 ID 和被关注者 ID 查找关注记录
    @Select("SELECT * FROM follows WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    Follow findByFollowerIdAndFollowedId(Integer followerId, Integer followedId);

    // 查找用户是否关注了某个用户
    @Select("SELECT COUNT(*) FROM follows WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int isUserFollowing(@Param("followerId") Integer followerId, @Param("followedId") Integer followedId);

    // 用户关注某人
    @Insert("INSERT INTO follows (follower_id, followed_id) VALUES (#{followerId}, #{followedId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void followUser(Follow follow);

    // 用户取消关注某人
    @Delete("DELETE FROM follows WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    void unfollowUser(Integer followerId, Integer followedId);

    // 查询关注了该用户的人数
    @Select("SELECT COUNT(DISTINCT follower_id) FROM follows WHERE followed_id = #{userId}")
    int countFollowersByUserId(int userId);

    // 查询用户关注的人数
    @Select("SELECT COUNT(DISTINCT followed_id) FROM follows WHERE follower_id = #{userId}")
    int countFollowedByUserId(int userId);

    // 查是否有记录
    @Select("SELECT COUNT(*) FROM follows WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    Integer countFollowersByFollowerId(Integer followerId , Integer followedId);
}
